package com.cskaoyan.service;

import com.cskaoyan.bean.User;
import com.cskaoyan.bean.UserData;
import com.cskaoyan.bean.bo.UserUpdateBo;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.po.*;
import com.cskaoyan.bean.vo.*;
import com.cskaoyan.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
// import org.springframework.validation.annotation.Validated;
// import sun.plugin2.message.PrintAppletReplyMessage;

import java.util.ArrayList;
import java.util.List;

/**
 * @author stone
 * @date 2022/01/06 16:36
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public UserData query(BaseParam param, String username, String mobile) {
        // 在执行查询之前，可以开启分页
        // sql语句预编译的过程给你拼接上分页的sql
        PageHelper.startPage(param.getPage(), param.getLimit());
        // 使用的userMapper中并没有提供page和limit
        List<User> users = userMapper.select(param.getSort(), param.getOrder(), username, mobile);
        // 会去获得一些分页信息
        // 上面的查询对应的PageInfo → 把原始查询结果放入到构造方法里
        PageInfo pageInfo = new PageInfo(users);

        // total是总的数据量 → 是否等于users.length?不是 → 指的是如果不分页的情况下最多会查询出来多少条记录
        // 按照上面的查询的查询条件执行的select count(*)
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return UserData.data(total, pages, param.getLimit(), param.getPage(), users);
    }

    /**
     * 查询Board的数据
     *
     * @return BoardVo
     */
    @Override
    public BoardVo queryCountForBoard() {
        BoardPo boardPo = userMapper.selectCountForBoard();
        BoardVo boardVo = new BoardVo(boardPo.getGoodsTotal(), boardPo.getUserTotal(), boardPo.getProductTotal(), boardPo.getOrderTotal());
        return boardVo;
    }

    /**
     * selectUserInfoById
     *
     * @param id
     * @return
     */
    @Override
    public UserDetailVo queryUserDetailById(Integer id) {
        UserDetailPo userDetailPo = userMapper.selectUserDetailById(id);
        UserDetailVo userDetailVo = new UserDetailVo();
        if (userDetailPo != null) {
            userDetailVo = new UserDetailVo(userDetailPo.getId(), userDetailPo.getUsername(), userDetailPo.getPassword(),
                    userDetailPo.getGender(), userDetailPo.getLastLoginTime(), userDetailPo.getLastLoginIp(),
                    userDetailPo.getUserLevel(), userDetailPo.getNickname(), userDetailPo.getMobile(),
                    userDetailPo.getAvatar(), userDetailPo.getWeixinOpenid(), userDetailPo.getSessionKey(),
                    userDetailPo.getStatus(), userDetailPo.getAddTime(), userDetailPo.getUpdateTime(), userDetailPo.isDeleted());
        } else {
            userDetailVo.setId(-1);
        }
        return userDetailVo;
    }

    @Override
    public TVO queryAddress(BaseParam param, Integer userId, String name) {
        PageHelper.startPage(param.getPage(), param.getLimit());
        List<AddressPo> addressPos = userMapper.selectAddress(param.getSort(), param.getOrder(), userId, name);
        PageInfo pageInfo = new PageInfo(addressPos);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return TVO.data(total, pages, param.getLimit(), param.getPage(), addressPos);
    }

    @Override
    public TVO queryCollect(BaseParam param, Integer userId, Integer valueId) {
        PageHelper.startPage(param.getPage(), param.getLimit());
        List<CollectPo> collectPos = userMapper.selectCollect(param.getSort(), param.getOrder(), userId, valueId);
        PageInfo pageInfo = new PageInfo(collectPos);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return TVO.data(total, pages, param.getLimit(), param.getPage(), collectPos);
    }

    @Override
    public TVO queryFootprint(BaseParam param, Integer userId, Integer goodsId) {
        PageHelper.startPage(param.getPage(), param.getLimit());
        List<FootprintPo> footprintPos = userMapper.selectFootprint(param.getSort(), param.getOrder(), userId, goodsId);
        PageInfo pageInfo = new PageInfo(footprintPos);
        return TVO.data(pageInfo.getTotal(), pageInfo.getPages(), param.getLimit(), param.getPage(), footprintPos);
    }

    @Override
    public TVO queryHistory(BaseParam param, Integer userId, String keyword) {
        PageHelper.startPage(param.getPage(), param.getLimit());
        List<HistoryPo> historyPos = userMapper.selectHistory(param.getSort(), param.getOrder(), userId, keyword);
        PageInfo pageInfo = new PageInfo(historyPos);
        return TVO.data(pageInfo.getTotal(), pageInfo.getPages(), param.getLimit(), param.getPage(), historyPos);
    }

    @Override
    public TVO queryFeedback(BaseParam param, String username, Integer id) {
        PageHelper.startPage(param.getPage(), param.getLimit());
        List<FeedbackPo> feedbackPos = userMapper.selectFeedback(param.getSort(), param.getOrder(), id, username);
        PageInfo pageInfo = new PageInfo(feedbackPos);
        return TVO.data(pageInfo.getTotal(), pageInfo.getPages(), param.getLimit(), param.getPage(), feedbackPos);
    }

    @Override
    public TVO queryRegion() {
        Integer total = userMapper.selectProvinceCount();
        Integer pages = 1;
        List<RegionPo> regionPos = userMapper.selectRegion();
        return TVO.data(total, pages, total, pages, regionPos);
    }

    @Override
    public void updateUserInfo(UserUpdateBo userUpdateBo) {

        System.out.println("-------------------------");
        System.out.println(userUpdateBo);
        System.out.println("-------------------------");
        if (userUpdateBo.getWeixinOpenid() == null) {
            userUpdateBo.setWeixinOpenid("");
        }
        if (userUpdateBo.getSessionKey() == null) {
            userUpdateBo.setSessionKey("");
        }
        userMapper.updateUserInfo(userUpdateBo);
    }

}
